using System;
using System.Collections;
using System.Text;
using System.Data;

namespace IndoorInformation
{
    public class Room : DoiTuong
    {
        #region string Name property
        public string Name
        {
            get
            {
                if (_DataRow["Name"] is string) return (string)_DataRow["Name"];
                return "";
            }
            set
            {
                _DataRow["Name"] = value;
            }
        }
        #endregion

        #region double X1 property
        public double X1
        {
            get
            {
                if (_DataRow["X1"] is double) return (double)_DataRow["X1"];
                return -1;
            }
            set
            {
                _DataRow["X1"] = value;
            }
        }
        #endregion

        #region double Y1 property
        public double Y1
        {
            get
            {
                if (_DataRow["Y1"] is double) return (double)_DataRow["Y1"];
                return -1;
            }
            set
            {
                _DataRow["Y1"] = value;
            }
        }
        #endregion

        #region double X2 property
        public double X2
        {
            get
            {
                if (_DataRow["X2"] is double) return (double)_DataRow["X2"];
                return -1;
            }
            set
            {
                _DataRow["X2"] = value;
            }
        }
        #endregion

        #region double Y2 property
        public double Y2
        {
            get
            {
                if (_DataRow["Y2"] is double) return (double)_DataRow["Y2"];
                return -1;
            }
            set
            {
                _DataRow["Y2"] = value;
            }
        }
        #endregion

        #region double X3 property
        public double X3
        {
            get
            {
                if (_DataRow["X3"] is double) return (double)_DataRow["X3"];
                return -1;
            }
            set
            {
                _DataRow["X3"] = value;
            }
        }
        #endregion

        #region double Y3 property
        public double Y3
        {
            get
            {
                if (_DataRow["Y3"] is double) return (double)_DataRow["Y3"];
                return -1;
            }
            set
            {
                _DataRow["Y3"] = value;
            }
        }
        #endregion

        #region double X4 property
        public double X4
        {
            get
            {
                if (_DataRow["X4"] is double) return (double)_DataRow["X4"];
                return -1;
            }
            set
            {
                _DataRow["X4"] = value;
            }
        }
        #endregion

        #region double Y4 property
        public double Y4
        {
            get
            {
                if (_DataRow["Y4"] is double) return (double)_DataRow["Y4"];
                return -1;
            }
            set
            {
                _DataRow["Y4"] = value;
            }
        }
        #endregion

        public Room(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucRoom : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucRoom _Instance;
        public static DanhMucRoom Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucRoom();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Room";
            }
        }
        #endregion 

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Room((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Room this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Room(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Room NewDoiTuong()
        {
            try
            {
                return new Room(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Room GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new Room(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Room GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucRoom GetAll() {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
